package com.monkey.controller;

import com.monkey.core.ErrorCode;
import com.monkey.model.luck.LuckData;
import com.monkey.model.user.User;
import com.monkey.service.LuckService;
import com.monkey.service.UserService;
import com.monkey.util.WebConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * desc:
 * auth: chenxiaobin
 * data: 2018/1/16.
 */
@RestController
@RequestMapping(value = "/luck")
public class LuckController {
    private static Logger logger = LoggerFactory.getLogger(LuckController.class);

    @Resource
    private UserService userService = null;

    @Resource
    private LuckService luckService = null;

    @Resource
    private WebConfig webConfig = null;

    /**
     * 获取抽奖信息
     * @return price int 抽奖金额
     */
    @RequestMapping(value = "/getInfo", method = RequestMethod.POST)
    public Map<String, Object> getInfo_log(@RequestBody Map<String, Object> params, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        if (null == token || token.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        User user = userService.getUserWithToken(token);
        if (null == user) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        int price = webConfig.getInt("luck.money");
        ret.put("code", ErrorCode.SUCCESS);
        ret.put("msg", "");
        ret.put("price", price);
        return ret;
    }

    /**
     * 抽奖接口
     * @return id int 宠物编号
     * @return g int 代数
     */
    @RequestMapping(value = "/do", method = RequestMethod.POST)
    public Map<String, Object> do_log(@RequestBody Map<String, Object> params, @RequestHeader String token) {
        Map<String, Object> ret = new HashMap<>();
        if (null == token || token.isEmpty()) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        User user = userService.getUserWithToken(token);
        if (null == user) {
            ret.put("code", ErrorCode.TOKEN_ERROR);
            ret.put("msg", ErrorCode.getMsg(ErrorCode.TOKEN_ERROR));
            return ret;
        }
        int price = (int) params.get("price");
        LuckData ret1 = luckService.doLuck(user, price);
        ret.put("code", ret1.getCode());
        ret.put("msg", ret1.getMsg());
        ret.put("id", ret1.getId());
        ret.put("g", ret1.getGeneration());
        return ret;
    }
}
